package servers;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import javax.persistence.EntityManager;

import factory.JPAFactory;

import valueobjects.PerifericoVO;

public class PerifericoRMIServer 
	extends UnicastRemoteObject 
		implements PerifericoRMI {
	
	public PerifericoRMIServer() 
		throws RemoteException{
		super();
	}

	@Override
	public ArrayList<PerifericoVO> obtemListaPerifericos()
		throws RemoteException {
		
		EntityManager manager = 
			JPAFactory.getInstance()
				.getJPAFactory()
					.createEntityManager();
		
		String sql = 
			"select object(o) " +
				"from PerifericoVO as o";
		
		ArrayList<PerifericoVO> retorno = (ArrayList<PerifericoVO>)
			manager.createQuery(sql).getResultList();

		return retorno;
	}
	
	public static void main(String[] args) {
		try {
			
			PerifericoRMIServer server =
				new PerifericoRMIServer();
			
			Naming.rebind("PerifericoRemoto", server);
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}
